<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
                 label-width="80px">
            <el-form-item label="类型" prop="type">
                <el-input v-model="dataForm.type" placeholder="类型"></el-input>
            </el-form-item>
            <el-form-item label="主题" prop="title">
                <el-input v-model="dataForm.title" placeholder="主题"></el-input>
            </el-form-item>
            <el-form-item label="邮箱模板" prop="template">
                <summer-note v-model="dataForm.template" :summerNoteVal="summerNoteVal"/>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    import SummerNote from "@/components/SummerNote";
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: 0,
                    type: '',
                    title: '',
                    template: '',
                },
                dataRule: {
                    type: [
                        {required: true, message: '类型不能为空', trigger: 'blur'}
                    ],
                    title: [
                        {required: true, message: '主题不能为空', trigger: 'blur'}
                    ],
                    template: [
                        {required: true, message: '邮箱模板不能为空', trigger: 'blur'}
                    ],
                },
                summerNoteVal:''
            }
        },
        components:{SummerNote},
        methods: {
            init(id) {
                this.summerNoteVal = "";
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        // /blog_user/emailtemplate/info/
                        this.$api.EmailTemplate.info(this.dataForm.id).then((data) => {
                            if (data && data.code === 0) {
                                this.dataForm.type = data.emailTemplate.type
                                this.dataForm.title = data.emailTemplate.title
                                this.dataForm.template = data.emailTemplate.template

                                this.summerNoteVal = data.emailTemplate.template
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        let data = {
                            'id': this.dataForm.id || undefined,
                            'type': this.dataForm.type,
                            'title': this.dataForm.title,
                            'template': this.dataForm.template,
                        };
                        let resultAction;
                        // /blog_user/emailtemplate/${!this.dataForm.id
                        if (!this.dataForm.id) {
                            resultAction = this.$api.EmailTemplate.save(data);
                        } else {
                            resultAction = this.$api.EmailTemplate.update(data);
                        }
                        resultAction.then((data) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
